<?php

use yii\grid\GridView;
use yii\bootstrap\Progress;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;

/* @var $this yii\web\View */
/* @var $searchModel app\models\search\ArcTrainSearchModel */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '进度';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="arc-train-index">

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'showFooter' => true,  //设置显示最下面的footer
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'id',
                'filter' => '<input type="text" class="form-control" placeholder="查询" name="ArcTrainSearchModel[id]">',
                'options' => [
                    'width' => '5%',
                ]
            ],
            [
                'attribute' => 'arc_classify',
                'filter' => Html::activeDropDownList($searchModel,
                    'arc_classify', ArrayHelper::map((new \app\models\ArcClassify())->find()->all(), 'id', 'name'),
                    ['prompt' => '全部', 'class' => 'form-control']),
                'footer' => '<input id="num"  type="number" href="#" class="btn btn-default btn-xs btn-delete" placeholder="每页显示的条数"/>',
                'value' =>
                    function ($model) {
                        return $model->getArcClassify()->one()->name;
                    },
                'options' => [
                    'width' => '15%',
                ]
            ],
            [
                'attribute' => 'arc_android',
                //通过这种方法滤掉更方便
                'filter' => Html::activeDropDownList($searchModel,
                    'arc_android', ArrayHelper::map((new \app\models\ArcAndroid())->find()->all(), 'id', 'name'),
                    ['prompt' => '全部', 'class' => 'form-control']),
                'value' =>
                    function ($model) {
                        ArrayHelper::map($model->find()->all(), 'arc_android', 'id');
                        return $model->getArcAndroid()->one()->name .
                            "<span class='badge bg-light-blue'>拷贝机器人" . $model->arc_android_num . "</span>";
                    },
                'format' => 'raw',//表示显示的是html格式的
                'options' => [
                    'width' => '20%',
                ]
            ],
            [
                'label' => '进度',
                'attribute' => 'progress',
                'filter' => Html::activeDropDownList($searchModel,
                    'progress', [0 => '未进行训练', 1 => '正在训练', 2 => '训练完成',3 => '训练中止'],
                    ['prompt' => '全部', 'class' => 'form-control']),
                'value' =>
                    function ($model, $key, $index, $column) {
                        $androidNumber = $model->getArcAndroid()->one()->onetime_num;
                        $percent = round($model->progress_num / $androidNumber * 100,6);
                        if ($model->progress == 1) {
                            return Progress::widget([
                                'percent' => $percent,
                                'label' => $percent . '%正在训练',
                                'barOptions' => [
                                    'class' => 'progress-bar-info progress-text-color',
                                ],
                                'options' => [
                                    'title' => $percent . '%正在训练',
                                    'class' => 'progress',
                                ]
                            ]);
                        } else if ($model->progress == 3) {
                            return Progress::widget([
                                'percent' => $percent,
                                'label' => "<div style='color:#dd4b39'>".$percent . '%训练已被中止'."</div>",
                                'barOptions' => [
                                    'class' => 'progress-bar-info progress-text-color',
                                ],
                                'options' => [
                                    'title' => $percent . '%训练已被中止',
                                    'class' => 'progress',
                                ]
                            ]);
                        }else if ($model->progress == 2) {
                            return Progress::widget([
                                'percent' => 100,
                                'label' => '100%训练已完成',
                                'barOptions' => [
                                    'title' => '100%训练已完成',
                                    'class' => 'progress-bar-success progress-text-color',
                                ],
                            ]);
                        } else if ($model->progress == 0) {
                            return Progress::widget([
                                'label' => '未开始进行训练',
                                'barOptions' => [
                                    'title' => '未开始进行训练',
                                    'class' => 'progress-bar-warning progress-text-color',
                                ],
                            ]);
                        } else {
                            return '<div href="#" style="color:#dd4b39">未能识别状态，请联系管理员</div>';
                        }
                    },
                'format' => 'raw',//表示显示的是html格式的
                'options' => [
                    'width' => '20%',
                ]
            ],
            ['attribute' => 'user',
                'value' =>
                    function($model){
                        if (!empty($model)){
                            $user = \common\models\User::findOne($model->user);
                            $username = "";
                            if(!empty($user)){
                                $username = $user->username;
                                return $username."";
                            }else{
                                return $username."";
                            }
                        }else{
                            return "";
                        }
                    },
                'label'=>'训练用户',
                'format' => 'raw',//表示显示的是html格式的
                'options'=>[
                    'width'=>'10%',
                ]
            ],
            ['attribute' => 'create_time',
                'value' =>
                    function($model){
                        if (!empty($model)){
                            return date("Y年m月d日 H:i:s",$model->create_time);
                        }else{
                            return "";
                        }
                    },
                'label'=>'创建时间',
                'format' => 'raw',//表示显示的是html格式的
                'options'=>[
                    'width'=>'14%',
                ]
            ],
            ['class' => 'yii\grid\ActionColumn',//下面是新增加的按按钮
                'header' => '操作',
                'template' => '{play}  {delete} ',
                'buttons' => [
                    'play' => function ($url, $model, $key) {
                        $arcTrainModel = \app\models\ArcTrain::findOne($model->id);
//                        if($arcTrainModel->progress==0){//未进行训练
                            $resStr = '<a id="modal_' . $arcTrainModel->arc_android_num . '" class="btn btn-success fa fa-play" href="../thing-record-file/classify-continue?serialNumber=' . $arcTrainModel->progress_num . '&arcAndroidId=' . $arcTrainModel->arc_android . '&arcTrainId=' . $arcTrainModel['id'] . '&train_progress=0" title="开始该训练" data-toggle="modal" data-target="#create-modal">训练</a>';
//                        }else if($arcTrainModel->progress==1){//正在训练
//                            $resStr = '<a id="modals_' . $arcTrainModel->arc_android_num . '" class="glyphicon glyphicon-pause" href="#" src="../thing-record-file/classify-stop?arcTrainId=' . $arcTrainModel->id . '" title="中止该训练"></a>';
//                        }else if($arcTrainModel->progress==2){//训练完成
//                            $resStr = '';
//                        }else if($arcTrainModel->progress==3){//训练中止
//                            $resStr = '<a id="modalp_' . $arcTrainModel->arc_android_num . '" class="glyphicon glyphicon-play" href="#" src="../thing-record-file/classify-continue?&arcTrainId=' . $arcTrainModel->id . '" title="继续该训练"></a>';
//                        }
                        return $resStr;
                    },
                    'delete'=> function ($url, $model, $key) {
                        return '<a href="'.$url.'" title="删除" aria-label="删除" data-pjax="0" data-confirm="您确定要删除此项吗？删除此项会删除相应的电话号码记录。" data-method="post"><span class="btn btn-danger fa fa-remove">删除</span></a>';
                    },
                ],
                'options' => [
                    'width' => '20%',
                ],
            ]
        ],
        'pager' => [
            'firstPageLabel' => "第一页",
            'lastPageLabel' => '最后一页',
        ],
    ]); ?>
</div>
<?= $this->render('/common/dialog_iframe')?>
<script type="text/javascript">
    $('#num').focusout(function () {//当鼠标失去焦点是触发事件,实现用户自定义分页
        var url = "<?= 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ?>";
        $.ajax({
            type: 'post',
            async: true,
            url: url,
            data: {pageSize: $("#num").val()},
            success: function (data) {
                window.location.reload();
            },
            error: function (error) {

            }
        });
    })
    //弹窗的实现
    var labelButton = $("#android_id").parent().next().children();
    var labelButtonText = labelButton.text();
    labelButton.html(labelButtonText);

    $("a[id^='modals_']").click(function () {
        var res = confirm("确定要中止该训练！");
        if (res){
            var src = $(this).attr("src");
            $.ajax({
                type: 'get',
                url: src,
                success: function (data) {
                    if(data == '1' || data=="1" || data==1){
                        alert("中止成功！");
                    }else{
                        alert("中止失败！");
                    }
                },
                error: function (error) {
                    alert("中止失败！");
                }
            });
        }
    });

    $("a[id^='modalp_']").click(function () {
        var res = confirm("确定要继续该训练！");
        if (res){
            var src = $(this).attr("src");
            $.ajax({
                type: 'get',
                url: src,
                success: function (data) {
                    if(data == '1' || data=="1" || data==1){
                        alert("继续训练动作成功！");
                    }else{
                        alert("继续训练动作失败！");
                    }
                },
                error: function (error) {
                    alert("继续训练动作失败！");
                }
            });
        }
    });
</script>
